package com.youkeda.redpacket.service.impl;

import com.youkeda.redpacket.dao.UserRedEnvelopeRecordDAO;
import com.youkeda.redpacket.dataobject.UserRedEnvelopeRecordDO;
import com.youkeda.redpacket.model.UserRedEnvelopeRecord;
import com.youkeda.redpacket.params.UserRedEnvelopeRecordQueryParam;
import com.youkeda.redpacket.service.UserRedEnvelopeRecordService;
import com.youkeda.redpacket.util.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 刘正星
 * @version 1.0.0
 * @ClassName UserRedEnvelopeRecordServiceImpl.java
 * @Description TODO
 * @createTime 2020年09月29日 14:31:00
 */
@Service
public class UserRedEnvelopeRecordServiceImpl implements UserRedEnvelopeRecordService {
    @Autowired
    private UserRedEnvelopeRecordDAO redEnvelopeRecordDAO;
    @Override
    public UserRedEnvelopeRecord save(UserRedEnvelopeRecord userRedEnvelopeRecord) {
        if (userRedEnvelopeRecord == null){
            return null;
        }

        if (StringUtils.isEmpty(userRedEnvelopeRecord.getId())){

            UserRedEnvelopeRecordDO userRedEnvelopeRecordDO = new UserRedEnvelopeRecordDO(userRedEnvelopeRecord);
            userRedEnvelopeRecordDO.setId(UUIDUtils.getUUID());
            int insertSize = redEnvelopeRecordDAO.insert(userRedEnvelopeRecordDO);
            if (insertSize < 1){
                return  null;
            }
            userRedEnvelopeRecord.setId(userRedEnvelopeRecordDO.getId());
        }else{
            UserRedEnvelopeRecordDO userRedEnvelopeRecordDO = new UserRedEnvelopeRecordDO(userRedEnvelopeRecord);
          int updateSize =  redEnvelopeRecordDAO.updateByPrimaryKeySelective(userRedEnvelopeRecordDO);
          if (updateSize < 1){
              return null;
          }
        }

        return userRedEnvelopeRecord;
    }

    @Override
    public List<UserRedEnvelopeRecord> query(UserRedEnvelopeRecordQueryParam param) {
        if (param == null){
            return  null;
        }
        List<UserRedEnvelopeRecordDO> userRedEnvelopeRecordDOS = redEnvelopeRecordDAO.select(param);
        List<UserRedEnvelopeRecord> userRedEnvelopeRecords = new ArrayList<>();
        userRedEnvelopeRecordDOS.forEach(userRedEnvelopeRecordDO -> {
            userRedEnvelopeRecords.add(userRedEnvelopeRecordDO.convertToModel());
        });
        return userRedEnvelopeRecords;
    }

    @Override
    public UserRedEnvelopeRecord get(String id) {
        if (StringUtils.isEmpty(id)){
            return null;
        }
        UserRedEnvelopeRecord userRedEnvelopeRecord = redEnvelopeRecordDAO.selectByPrimaryKey(id).convertToModel();
        return null;
    }
}
